package com.yuyou.fn.educational.service.impl;

import com.github.wens.framework.service.impl.SuperServiceImpl;
import com.github.wens.mybatisplus.examples.Example;
import com.yuyou.fn.common.util.BeanUtils;
import com.yuyou.fn.common.util.TimeUtils;
import com.yuyou.fn.educational.entity.StudentGuardian;
import com.yuyou.fn.educational.mapper.StudentGuardianMapper;
import com.yuyou.fn.educational.service.IStudentGuardianService;
import com.yuyou.fn.educational.vo.StudentGuardianSaveOrUpdateVo;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 *
 * StudentGuardian 表数据服务层接口实现类
 *
 */
@Service
public class StudentGuardianServiceImpl extends SuperServiceImpl<StudentGuardianMapper, StudentGuardian,Long> implements IStudentGuardianService {


    @Override
    public void saveOrUpdateGuardian(Long studentId , List<StudentGuardianSaveOrUpdateVo> guardianList) {

        if(studentId == null ){
            throw new NullPointerException();
        }

        Example<StudentGuardian> example = StudentGuardian.createExample();
        example.selectProperties("guardianId");
        example.createCriteria().andEqualTo("studentId" , studentId ) ;
        deleteByExample(example);


        List<StudentGuardian> list  = new ArrayList<>(guardianList.size());
        for(StudentGuardianSaveOrUpdateVo vo : guardianList ){
            vo.setStudentId(studentId);
            StudentGuardian studentGuardian = new StudentGuardian();
            BeanUtils.copyProperties(studentGuardian, vo );
            studentGuardian.setCreateTime(TimeUtils.now());
            studentGuardian.setUpdateTime(TimeUtils.now());
            list.add(studentGuardian);
        }
        insertBatch(list) ;

    }

    @Override
    public List<StudentGuardian> findGuardianByStudentId(Long studentId) {
        Example<StudentGuardian> example = StudentGuardian.createExample();
        example.createCriteria().andEqualTo("studentId" , studentId ) ;
        return findListByExample(example);
    }
}